<template>
  <div class="setRecharge">
    <div class="center-w">
      <div class="head">
        <div class="title">充值数额</div>
        <div class="btn">
          <el-button type="primary" @click="addCheckItem">添加数额</el-button>
        </div>
      </div>
      <el-table size="mini" :data="checkList">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column prop="amount" align="center" label="充值金额">
          <template slot-scope="scope">
            <el-input
              style="width: 180px;"
              show-word-limit
              placeholder="请输入"
              v-model="scope.row.amount"
              clearable
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="giftAmount" align="center" label="赠送金额">
          <template slot-scope="scope">
            <el-input
              style="width: 180px;"
              show-word-limit
              oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"
              @blur="scope.row.giftAmount = $event.target.value"
              placeholder="请输入"
              v-model="scope.row.giftAmount"
              clearable
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column width="180" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="submit(scope.row)"
              :type="scope.row.id ? 'primary' : 'success'"
              >{{ scope.row.id ? "编辑" : "新增" }}</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="deleteCurrentItem(scope.row, scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  getRechargeList,
  addNewRecharge,
  updateRecharge,
  delectRecharge
} from "../../api/recharge";
export default {
  data() {
    return {
      checkList: [],
      otherObj: {}
    };
  },
  name: "setRecharge",
  created() {
    this._getRechargeList();
  },

  methods: {
    // 添加类目
    addCheckItem() {
      this.checkList.push({
        amount: null,
        giftAmount: null
      });
    },

    // 获取充值数额列表
    async _getRechargeList() {
      let result = await getRechargeList();
      if (result.code == 200) {
        this.checkList = result.data;
      } else {
        this.$message.error(result.message);
      }
    },

    // 删除某个选项
    deleteCurrentItem(e, index) {
      this.$confirm("您确定要删除吗？", "删除确认", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定删除",
        cancelButtonText: "取消"
      })
        .then(() => {
          //这里判断是否有id 有的话就根据接口来处理删除，没有的话就按照下标来删除。
          if (e.id) {
            delectRecharge({ id: e.id }).then(res => {
              if (res.code == 200) {
                this.$message.success("删除成功");
                this._getRechargeList();
              } else {
                this.$message.error(res.message);
                this._getRechargeList();
              }
            });
          } else {
            this.checkList.splice(index, 1);
          }
        })
        .catch(action => {
          this.$message.info("取消删除");
        });
    },

    // 编辑 添加
    async submit(r) {
      let result = [];
      if (r.id) {
        result = await updateRecharge(r);
      } else {
        result = await addNewRecharge(r);
      }
      if (result.code == 200) {
        this.$message.success(`${r.id ? "编辑成功" : "新增成功"}`);
        this._getRechargeList();
      } else {
        this.$message.error(result.message);
      }
    }
  }
};
</script>

<style lang="less" scoped>
.center-w {
  display: flex;
  /* align-items: center; */
  flex-direction: column;
  background-color: #fff;
  height: 85vh;
  overflow-y: scroll;
  /* min-height: 1150px; */
  padding: 20px;
  border: 1px solid #ccc;
  margin: 20px;
  .title {
    color: rgba(16, 16, 16, 1);
    font-size: 20px;
    margin-bottom: 10px;
  }

  .head {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .list-w {
    display: flex;
    align-items: center;
    /* border: 1px solid #f4f1f1; */
  }

  .sex div {
    color: rgba(109, 109, 109, 1);
    font-size: 16px;
    font-weight: 500;
  }

  .sex div:first-child {
    width: 150px;
    text-align: center;
  }

  .btn-s {
    margin-left: 35%;
    margin-top: 50px;
  }
  .names {
    color: rgba(16, 16, 16, 1);
    font-size: 20px;
    width: 250px;
    text-align: center;
    font-weight: 500;
  }

  .el-input {
    max-width: 300px;
  }

  .sex {
    display: flex;
    align-items: center;
    margin: 20px;
  }

  .btn {
    width: 200px;
    text-align: right;
  }
}
</style>
